package com.yike.order.lkl.api;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.V3SacsFallbackRequest;
import com.lkl.laop.sdk.request.V3SacsQueryRequest;
import com.lkl.laop.sdk.request.V3SacsSeparateRequest;
import com.lkl.laop.sdk.request.model.V3SacsSeparateRecvDatas;
import com.yike.common.Response.LklPayResponse;
import com.yike.common.Response.LklResponse;
import com.yike.common.Response.LklSacsFallbackResponse;
import com.yike.common.Response.LklSacsSeparateResponse;
import com.yike.common.entity.OrderLklSeparate;
import com.yike.order.lkl.demo.BaseCommonDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Administrator
 * @description: 订单拉卡拉分账相关
 */
@Slf4j
@Component
public class V3SacsSeparateApi  extends BaseCommonDemo {



    @Value("${lkl.org_code}")
    private String  orgCode;
    @Value("${lkl.not_url}")
    private String  notUrl;


    /**
     * 拉卡拉分账
     * @param orderLklSeparate
     * @return LklSacsSeparateResponse
     * @throws SDKException
     * @throws IOException
     */
    public LklSacsSeparateResponse sacsSeparate(OrderLklSeparate orderLklSeparate, String receiverNo) throws SDKException, IOException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

        V3SacsSeparateRequest v3SacsSeparateRequest = new V3SacsSeparateRequest();
        v3SacsSeparateRequest.setMerchantNo(orderLklSeparate.getMerchantNo());
        v3SacsSeparateRequest.setLogNo(orderLklSeparate.getLogNo());
        v3SacsSeparateRequest.setLogDate( simpleDateFormat.format(orderLklSeparate.getLogDate()));

        v3SacsSeparateRequest.setOutSeparateNo(orderLklSeparate.getOutSeparateNo());
        v3SacsSeparateRequest.setTotalAmt(String.valueOf(orderLklSeparate.getTotalAmt()));
        v3SacsSeparateRequest.setCalType(String.valueOf(orderLklSeparate.getCalType()));
        v3SacsSeparateRequest.setLklOrgNo(this.orgCode);
        v3SacsSeparateRequest.setNotifyUrl(this.notUrl + "/api-order/lkl/notice/sacsSeparateNot");

        List<V3SacsSeparateRecvDatas> list = new ArrayList<>();
        V3SacsSeparateRecvDatas v3SacsSeparateRecvDatas = new V3SacsSeparateRecvDatas();
        v3SacsSeparateRecvDatas.setRecvNo(receiverNo);
        v3SacsSeparateRecvDatas.setSeparateValue(String.valueOf(orderLklSeparate.getTotalAmt()));
        list.add(v3SacsSeparateRecvDatas);
        v3SacsSeparateRequest.setRecvDatas(list);
        log.info("发起拉卡拉分账-请求参数，orderLklSeparate:{}",JSON.toJSONString(v3SacsSeparateRequest));

        //3. 发送请求
        String response = LKLSDK.httpPost(v3SacsSeparateRequest);
        log.info("发起拉卡拉分账-返回内容，response:{}",response);

        LklResponse lklResponse = JSONObject.parseObject(response,LklResponse.class);
        if(!lklResponse.getCode().equals("SACS0000")){
            throw new RuntimeException(lklResponse.getMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), LklSacsSeparateResponse.class);

    }



    /**
     * 拉卡拉分账回退
     * @param orderLklSeparate
     * @return LklSacsSeparateResponse
     * @throws SDKException
     * @throws IOException
     */
    public LklSacsFallbackResponse sacsFallback(OrderLklSeparate orderLklSeparate, String receiverNo) throws SDKException, IOException {
        V3SacsFallbackRequest request = new V3SacsFallbackRequest();
        request.setMerchantNo(orderLklSeparate.getMerchantNo());
        request.setOriginSeparateNo(orderLklSeparate.getSeparateNo());
        request.setOutSeparateNo(orderLklSeparate.getFallbackOutSeparateNo());
        request.setTotalAmt(String.valueOf(orderLklSeparate.getTotalAmt()));

        List<V3SacsFallbackRequest.OriginRecvData> list = new ArrayList<>();
        V3SacsFallbackRequest.OriginRecvData originRecvData = new V3SacsFallbackRequest.OriginRecvData();
        originRecvData.setRecvNo(receiverNo);
        originRecvData.setAmt(String.valueOf(orderLklSeparate.getTotalAmt()));
        list.add(originRecvData);
        request.setOriginRecvDatas(list);

        log.info("发起拉卡拉分账回退-请求参数，orderLklSeparate:{}",JSON.toJSONString(orderLklSeparate));

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起拉卡拉分账回退-返回内容，response:{}",response);

        LklResponse lklResponse = JSONObject.parseObject(response,LklResponse.class);
        if(!lklResponse.getCode().equals("SACS0000")){
            throw new RuntimeException(lklResponse.getMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), LklSacsFallbackResponse.class);

    }



    /**
     * 拉卡拉分账结果查询
     * @param orderLklSeparate
     * @return LklSacsSeparateResponse
     * @throws SDKException
     * @throws IOException
     */
    public Map<String ,Object> queryPlus(OrderLklSeparate orderLklSeparate) throws SDKException, IOException {
        V3SacsQueryRequest request = new V3SacsQueryRequest();
        request.setMerchantNo(orderLklSeparate.getMerchantNo());
        request.setSeparateNo(orderLklSeparate.getSeparateNo());
        log.info("发起拉卡拉分账结果查询-请求参数，orderLklSeparate:{}",JSON.toJSONString(orderLklSeparate));

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起拉卡拉分账结果查询-返回内容，response:{}",response);

        LklResponse lklResponse = JSONObject.parseObject(response,LklResponse.class);
        if(!lklResponse.getCode().equals("SACS0000")){
            throw new RuntimeException(lklResponse.getMsg());
        }
        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), Map.class);


    }
}
